import time
import pymysql


def get_time():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


def get_connect():
    # conn = pymysql.connect(host='localhost', port=3306, database='china_data', user='root', password='huyaoyi123',
    #                        charset='utf8')
    conn = pymysql.connect(host='43.142.44.130', port=3306, database='data', user='data', password='huyaoyi123',
                           charset='utf8')
    return conn


def close_connect(cls, conn):
    cls.close()
    conn.close()


def select(sql):
    conn = get_connect()
    cls = conn.cursor()
    cls.execute(sql)
    result = cls.fetchall()
    close_connect(cls, conn)
    return result


def get_part_data():
    sql = 'select 省份,现有确诊 from province'
    return select(sql)


def get_30_total():
    sql = 'select 日期,累计确诊,累计死亡,累计治愈 from china_30'
    return select(sql)


def get_30_part():
    sql = 'select 日期,累计确诊,累计死亡,累计治愈 from 河南'
    return select(sql)


# 获得现有确诊人数排行前五名的数据
def get_top5():
    data = get_part_data()
    # 元祖转列表
    data = list(data)
    # 里面的元祖转列表
    data = [list(i) for i in data]
    # 把每个列表的第二个元素改成int类型，并以此排序
    data = sorted(data, key=lambda x: int(x[1]), reverse=True)
    # 取前五个
    data = data[:5]
    return data


if __name__ == '__main__':
    pass

